//
//  KKSplitAnimationModel.m
//  Butler
//
//  Created by liaozhenming on 16/6/17.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "KKSplitAnimationModel.h"

@interface KKSplitAnimationModel()

@property (nonatomic, strong) UIView *kk_pannelView;
@property (nonatomic, strong) UIImageView *kk_topView;
@property (nonatomic, strong) UIImageView *kk_bottomView;
@property (nonatomic, weak) UIView *kk_targetView;
@property (nonatomic, weak) UIView *kk_crackView;
@property (nonatomic, weak) UIControl *kk_control;
@property (nonatomic, assign) BOOL topViewAnimation;

@end

static CGFloat KKSplitArrowHeight = 20;
static NSTimeInterval KKAnimationDuration = 0.3;

@implementation KKSplitAnimationModel

+ (instancetype)splitAnimationModel {
    static KKSplitAnimationModel *itemModel = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        itemModel = [KKSplitAnimationModel new];
    });
    return itemModel;
}

#pragma mark - Public methods

+ (void)kk_splitAnimationWithTargetView:(UIView*)targetView eventControl:(UIControl*)control crackView:(UIView*)crackView {
    
    if (targetView == nil || control == nil || crackView == nil) {
        return;
    }
    
    KKSplitAnimationModel *splitAnimationModel = [KKSplitAnimationModel splitAnimationModel];
    splitAnimationModel.kk_crackView = crackView;
    
    UIImage *targetImage = [UIImage kk_screenImageWithLayer: targetView.layer];
    
    CGFloat crackViewHeight = CGRectGetHeight(crackView.bounds);
    CGFloat startPointY = CGRectGetMaxY(control.frame);
    CGFloat startPointX = CGRectGetMinX(control.frame) + CGRectGetWidth(control.bounds)/2;
    CGFloat pannelHeight = CGRectGetHeight(targetView.bounds);
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *backgroundView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, CGRectGetWidth(window.bounds), CGRectGetHeight(window.bounds))];
    backgroundView.backgroundColor = [UIColor kk_colorWithHexString: @"f0f0f0"];
    
    crackView.center = CGPointMake(CGRectGetWidth(backgroundView.bounds)/2, startPointY + CGRectGetHeight(crackView.bounds)/2 + KKSplitArrowHeight);
    CGAffineTransform transform = crackView.transform;
    crackView.transform = CGAffineTransformTranslate(transform, 1, 0);
    [backgroundView addSubview: crackView];
    
    UIImageView *topImageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, CGRectGetWidth(backgroundView.bounds), startPointY + KKSplitArrowHeight)];
    [topImageView setUserInteractionEnabled: YES];
    [topImageView addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget: splitAnimationModel action: @selector(on_Tap:)]];
    [backgroundView addSubview: topImageView];
    
    UIImageView *bottomImageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, startPointY - 1, CGRectGetWidth(backgroundView.bounds), pannelHeight - startPointY)];
    [bottomImageView setUserInteractionEnabled: YES];
    [bottomImageView addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget: splitAnimationModel action: @selector(on_Tap:)]];
    [backgroundView addSubview: bottomImageView];
    [targetView addSubview: backgroundView];
    
    splitAnimationModel.kk_pannelView = backgroundView;
    splitAnimationModel.kk_topView = topImageView;
    splitAnimationModel.kk_bottomView = bottomImageView;
    splitAnimationModel.kk_control = control;
    
    if (startPointY + crackViewHeight < pannelHeight) {
        //  顶部
        {
            NSMutableArray *points = [NSMutableArray array];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(startPointX, startPointY + KKSplitArrowHeight/2)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(startPointX + KKSplitArrowHeight/2, startPointY + KKSplitArrowHeight)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(CGRectGetWidth(targetView.bounds), startPointY + KKSplitArrowHeight)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(CGRectGetWidth(targetView.bounds), 0)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(0, 0)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(0, startPointY + KKSplitArrowHeight)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(startPointX - KKSplitArrowHeight/2, startPointY + KKSplitArrowHeight)]];
            
            UIImage *topImage = [targetImage kk_cutOutWithPoints: points];
            UIImage *cutImage = [topImage kk_cutOutWithFrame: CGRectMake(0, 0, CGRectGetWidth(targetView.bounds), startPointY + KKSplitArrowHeight)];
            topImageView.image = cutImage;
        }
        //  底部
        {
            NSMutableArray *points = [NSMutableArray array];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(startPointX, startPointY + KKSplitArrowHeight/2)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(startPointX + KKSplitArrowHeight/2, startPointY + KKSplitArrowHeight)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(CGRectGetWidth(targetView.bounds), startPointY + KKSplitArrowHeight)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(CGRectGetWidth(targetView.bounds), CGRectGetHeight(targetView.bounds))]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(0, CGRectGetHeight(targetView.bounds))]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(0, startPointY + KKSplitArrowHeight)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(startPointX - KKSplitArrowHeight/2, startPointY + KKSplitArrowHeight)]];
            
            UIImage *bottomImage = [targetImage kk_cutOutWithPoints: points];
            UIImage *cutImage = [bottomImage kk_cutOutWithFrame: CGRectMake(0, startPointY, CGRectGetWidth(targetView.bounds), CGRectGetHeight(targetView.bounds) - startPointY)];
            bottomImageView.image = cutImage;
        }
        
        //  动画
        splitAnimationModel.topViewAnimation = NO;
        [UIView animateWithDuration:KKAnimationDuration animations:^{
            bottomImageView.center = CGPointMake(CGRectGetWidth(bottomImageView.bounds)/2, startPointY + crackViewHeight +CGRectGetHeight(bottomImageView.bounds)/2);
            crackView.transform = CGAffineTransformIdentity;
        }];
    }
    else {
        startPointY = CGRectGetMinY(control.frame);
        
        //  顶部
        {
            
            NSMutableArray *points = [NSMutableArray array];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(startPointX, startPointY - KKSplitArrowHeight/2)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(startPointX + KKSplitArrowHeight/2, startPointY - KKSplitArrowHeight)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(CGRectGetWidth(targetView.bounds), startPointY - KKSplitArrowHeight)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(CGRectGetWidth(targetView.bounds), 0)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(0, 0)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(0, startPointY - KKSplitArrowHeight)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(startPointX - KKSplitArrowHeight/2, startPointY - KKSplitArrowHeight)]];
            
            UIImage *topImage = [targetImage kk_cutOutWithPoints: points];
            UIImage *cutImage = [topImage kk_cutOutWithFrame: CGRectMake(0, 0, CGRectGetWidth(targetView.bounds), startPointY + KKSplitArrowHeight)];
            topImageView.image = cutImage;
        }
        //  底部
        {
            //开始绘制图片

            NSMutableArray *points = [NSMutableArray array];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(startPointX, startPointY - KKSplitArrowHeight/2)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(startPointX + KKSplitArrowHeight/2, startPointY - KKSplitArrowHeight)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(CGRectGetWidth(targetView.bounds), startPointY - KKSplitArrowHeight)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(CGRectGetWidth(targetView.bounds), CGRectGetHeight(targetView.bounds))]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(0, CGRectGetHeight(targetView.bounds))]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(0, startPointY - KKSplitArrowHeight)]];
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(startPointX - KKSplitArrowHeight/2, startPointY - KKSplitArrowHeight)]];
            
            UIImage *bottomImage = [targetImage kk_cutOutWithPoints: points];
            UIImage *cutImage = [bottomImage kk_cutOutWithFrame: CGRectMake(0, startPointY, CGRectGetWidth(targetView.bounds), CGRectGetHeight(targetView.bounds) - startPointY)];
            bottomImageView.image = cutImage;
        }
    }
}

#pragma mark - Private methods

- (void)cleanAllCaches {
    
    [_kk_pannelView.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
    [_kk_pannelView removeFromSuperview];
}

- (void)on_Tap:(UITapGestureRecognizer*)tapper {
    
    if (self.topViewAnimation) {
        
    }
    else {
        __weak UIView* bottomView = self.kk_bottomView;
        __weak typeof(self) weakSelf = self;
        [UIView animateWithDuration:KKAnimationDuration animations:^{
            bottomView.center = CGPointMake(CGRectGetWidth(bottomView.bounds)/2, CGRectGetMaxY(weakSelf.kk_control.frame) + CGRectGetHeight(bottomView.bounds)/2);
            
            CGAffineTransform transform = weakSelf.kk_crackView.transform;
            weakSelf.kk_crackView.transform = CGAffineTransformTranslate(transform, 1, 0);
        } completion:^(BOOL finished) {
            [weakSelf cleanAllCaches];
        }];
    }
}

@end
